package com.up.stores.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.up.stores.entity.Product;
import com.up.stores.mapper.OrderMapper;
import com.up.stores.mapper.ProductMapper;
import com.up.stores.service.IProductService;
import com.up.stores.service.ex.CreateProductException;
import com.up.stores.service.ex.ProductListErrException;
import com.up.stores.service.ex.ProductNotFoundException;
import com.up.stores.service.ex.ProductUpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    OrderMapper orderMapper;
    @Override
    public Product findById(Integer id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pro_id",id);
        Product result=productMapper.selectOne(queryWrapper);
        if(result==null){
            //抛出异常
            throw new ProductNotFoundException("not found");
        }
        return result;
    }


    //todo图片上传的问题
    @Override
    public void ListPro(Product product,String modified_user) {
        //在商品表中添加就好了
        //加入改变一些参数
        product.setStatus(1);
        Date date=new Date();//更新时间
        product.setCreatedTime(date);
        product.setModifiedTime(date);
        product.setModifiedUser(modified_user);
        if(productMapper.updateById(product)!=1){
            //如果返回的不是1，那么就是上架错误了
            //抛出异常
            throw new ProductListErrException("List Error");
        }
        return;
    }

    @Override
    public void UnlistPro(Integer pro_id, String modified_user) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pro_id",pro_id);

        Product product=findById(pro_id);
        product.setStatus(2);
        Date date=new Date();//更新时间
        product.setModifiedTime(date);
        product.setModifiedUser(modified_user);
        //插入
        if(productMapper.update(product,queryWrapper)!=1){
            //返回值是0的时候是没有查到对应的数据
            //throw出去
            throw new ProductUpdateException("update err-product-not-found");
        }
        return;
    }

    @Override
    public List<Product> ShowMyProduct(Integer Bus_id) {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("bus_id",Bus_id);
        List<Product>list=new ArrayList<>();
        list=productMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Product UpdataProduct(Product product) {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        Integer id=product.getBusId();
        queryWrapper.eq("bus_id",id);
        productMapper.update(product,queryWrapper);
        return null;
    }


    //todo
    @Override
    public Product CreateProduct(Product product) {
        if(productMapper.insert(product)!=1)
        {
            throw new CreateProductException("failed to create");
        }

        return product;
    }

    @Override
    public List<Product> GetALLPro(int pageNum,int pageSize) {
        List<Product> products =new ArrayList<>();
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        products = productMapper.selectList(null);
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        return pageInfo.getList();
    }
}
